package com.smarttiger.zero.admin.service;

import com.smarttiger.zero.admin.dao.MenuDao;
import com.smarttiger.zero.admin.dao.RoleMenuDao;
import com.smarttiger.zero.admin.entity.MenuModel;
import com.smarttiger.zero.admin.entity.RoleMenuModel;
import com.smarttiger.zero.admin.util.ParamNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import java.util.*;

@Service
public class RoleMenuService extends BaseService<RoleMenuModel>{
    @Autowired
    RoleMenuDao dao;

    @Autowired
    MenuDao menuDao;

    @PostConstruct
    public void initMethod(){
        super.setDao(dao);
    }

    @ParamNames("groupId")
    public List<RoleMenuModel> getMenusByGroupId(String groupId){
        return getRoleMenusByGroupId(groupId,null);
    }

    @ParamNames("groupId")
    public List<String> getMenuIdsByGroupId(String groupId){
        List<RoleMenuModel> roleMenusByGroupId = getRoleMenusByGroupId(groupId, null);
        List<String> list = new ArrayList<>();
        forEach(roleMenusByGroupId,list);
        return list;
    }

    @Transactional
    public void saveRoleMenus(Map<String,Object> map){
        String groupId = map.get("groupId").toString();
        List<String> ids = ((List<String>) map.get("ids"));
        dao.deleteByGroupId(groupId);
        List<MenuModel> menus = menuDao.getMenuModelsByParentIdIsNull();

        MenuModel e = menus.get(0);
        RoleMenuModel  rmm = new RoleMenuModel();
        rmm.setId(UUID.randomUUID().toString());
        rmm.setGroupId(groupId);
        rmm.setParentMenuId(e.getParentId());
        rmm.setMenuId(e.getId());

        dao.save(rmm);
        forEachSave(menus,ids,groupId);
    }

    public void forEachSave(List<MenuModel> menus,List<String> ids,String groupId){
        menus.forEach(e -> {
            if(ids.contains(e.getId())){
                saveAndParent(e,groupId);
            }
            List<MenuModel> menuModelList = e.getMenuModelList();
            forEachSave(menuModelList,ids,groupId);
        });
    }

    public void saveAndParent(MenuModel e,String groupId){
        RoleMenuModel  rmm = new RoleMenuModel();
        rmm.setId(UUID.randomUUID().toString());
        rmm.setGroupId(groupId);
        rmm.setParentMenuId(e.getParentId());
        rmm.setMenuId(e.getId());
        dao.save(rmm);
        if(e.getParentId() != null){
            RoleMenuModel parentRoleMenu = dao.findByGroupIdAndMenuId(groupId, e.getParentId());
            if(parentRoleMenu == null){
                MenuModel one = menuDao.findById(e.getParentId()).get();
                saveAndParent(one,groupId);
            }
        }

    }

    public List<RoleMenuModel> getRoleMenusByGroupId(String groupId,String parentId){
        List<RoleMenuModel> allByGroupId = null;
        if(parentId == null){
           allByGroupId = dao.findAllByGroupIdAndParentMenuIdIsNull(groupId);
        }else {
           allByGroupId = dao.findAllByGroupIdAndParentMenuId(groupId,parentId);
        }
        allByGroupId.forEach(e -> {
            String menuId = e.getMenuId();
            List<RoleMenuModel> allByGroupId1 = getRoleMenusByGroupId(groupId,menuId);
            e.setRoleMenuModels(allByGroupId1);
        });
        return allByGroupId;
    }

    public void forEach(List<RoleMenuModel> roleMenuModels,List<String> list){
        roleMenuModels.forEach(e ->{
            if(e.getRoleMenuModels().size()==0){
                list.add(e.getMenuId());
            }
            if(e.getRoleMenuModels().size() >0){
                forEach(e.getRoleMenuModels(),list);
            }
        });
    }

}
